<?php

include_once "db.php";
include_once "chapter.php";

class User
{
    private $user_id;
    private $first_name;
    private $last_name;
    private $password;
    private $email;
    private $permissions;
    private $chapter;
    private $pictureid;

    private $userfound;

    //Constructor takes email and password of user plus the chapter object they belong to
    public function __construct($email, $password, $chapter, $hashed = false)
    {
        global $mysqli;
        $email = mysqli_real_escape_string($mysqli, $email);
        if ($hashed)
        {
            $password = mysqli_real_escape_string($mysqli, $password);
        } else
        {
            $password = mysqli_real_escape_string($mysqli, sha1($password));
        }
        $query = "SELECT * FROM Users WHERE Email = '" . $email . "' AND Password = '" .
            $password . "'";
        $data = $mysqli->query($query);
        if ($data->num_rows != 0)
        {
            $row = $data->fetch_array(MYSQLI_ASSOC);
            $this->first_name = $row["FirstName"];
            $this->last_name = $row["LastName"];
            $this->email = $row["Email"];
            $this->permissions = intval($row["PermissionID"]);
            $this->user_id = intval($row["UserID"]);
            $this->password = $row["Password"];
            $this->pictureid = intval($row["PictureID"]);
            $this->chapter = $chapter;
            $this->userfound = true;
        } else
        {
            $this->userfound = false;
        }
    }

    public function getFirstName()
    {
        return $this->first_name;
    }

    public function getLastName()
    {
        return $this->last_name;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getPermissions()
    {
        return $this->permissions;
    }

    public function getUserId()
    {
        return $this->user_id;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getPictureId()
    {
        return $this->pictureId;
    }

    public function getChapter()
    {
        return $this->chapter;
    }

    public function userFound()
    {
        return $this->userfound;
    }

    private function updateValue($value, $dbstring)
    {
        global $mysqli;
        $query = "UPDATE Users SET " . $dbstring . "='" . $value . "' WHERE UserId='" .
            $this->user_id . "'";
        $mysqli->query($query);
    }

    public function setFirstName($fname)
    {
        $this->updateValue($fname, "FirstName");
        $this->first_name = $fname;
    }

    public function setLastName($lname)
    {
        $this->updateValue($lname, "LastName");
        $this->last_name = $lname;
    }

    public function setEmail($email)
    {
        $this->updateValue($email, "Email");
        $this->email = $email;
    }

    public function setPermissions($permissions)
    {
        $this->updateValue($permissions, "Permission");
        $this->permissions = $permissions;
    }

    public function setPassword($password)
    {
        $this->updateValue(sha1($password), "Password");
        $this->password = sha1($password);
    }

    public function setPictureId($id)
    {
        $this->updateValue($password, "PictureId");
        $this->pictureid = $id;
    }

    //Displays the user's profile image
    public function displayImage($width, $height)
    {
        global $mysqli;
        global $root;
        //Use default
        if ($this->pictureid == -1)
        {
            echo "<img src=\"" . $root . "images/default-profile-img.png\" width=\"" . $width .
                "\" height=\"" . $height . "\" />";
        } else
        {
            echo "<img src=\"./getimage.php?id=" . $this->pictureid . " width=\"" . $width .
                "\" height=\"" . $height . "\" />";
        }
    }
}

?>